package common;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;

public class AreaView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int TILESIZE = 20;
	private static final int WALLSIZE = 3;
	private static final int MARGIN = 50;
	
	private Area area;
	
	public AreaView(Area area){
		this.area = area;
		this.setSize(MARGIN*2 + area.width * (TILESIZE+WALLSIZE-2), MARGIN*2 + area.length * (TILESIZE+WALLSIZE-1));
		this.setVisible(true);
	}
	
	public void paint(Graphics g) {
	    Graphics2D g2 = (Graphics2D)g;
	    
	    
	    
	    g2.setColor(Color.red);
	    
	    for (Room room : area.getRooms()){
	    	if (room.isEnabled()){
		    	for (RoomTile tile : room.getTiles()){
		    		g2.setColor(new Color(200-room.getValue()*2, 200-room.getValue()*2, 200-room.getValue()*2));
		    		if (room.keyID != 0) g2.setColor(getColor(room.keyID));
		    		if (tile.keyID != 0) g2.setColor(getColor(tile.keyID));
		    		if (tile.isEntrance()) g2.setColor(Color.blue);
		    		if (tile.isExit()) g2.setColor(Color.red);
		    		g2.fillRect(MARGIN + tile.sw.x * (TILESIZE+WALLSIZE) , this.getHeight() - (MARGIN + TILESIZE + tile.sw.y * (TILESIZE+WALLSIZE)), TILESIZE, TILESIZE);
		    	}
		    	
		    	for (Wall wall : room.getNoWalls()){
		    		g2.setColor(new Color(200-room.getValue()*2, 200-room.getValue()*2, 200-room.getValue()*2));
		    		if (room.keyID != 0) g2.setColor(getColor(room.keyID));
		    		drawWall(g2, wall);
		    	}
	    	}
	    }
	    
    	for (Wall wall : area.getDoors()){
    		g2.setColor(Color.yellow);
    		if (wall.keyID != 0) g2.setColor(getColor(wall.keyID));
    		drawWall(g2, wall);
    	}
	}
	
	public void drawWall(Graphics2D g2, Wall wall){
		if (wall.n1.x == wall.n2.x){
			if (wall.n1.y < wall.n2.y){
				g2.fillRect(MARGIN + wall.n1.x * (TILESIZE+WALLSIZE) - WALLSIZE, this.getHeight() - (MARGIN + TILESIZE + wall.n1.y * (TILESIZE+WALLSIZE)), WALLSIZE, TILESIZE);
			}
			else
			{
				g2.fillRect(MARGIN + wall.n2.x * (TILESIZE+WALLSIZE) - WALLSIZE, this.getHeight() - (MARGIN + TILESIZE + wall.n2.y * (TILESIZE+WALLSIZE)), WALLSIZE, TILESIZE);
			}
			
		}
		else
		{
			if (wall.n1.x < wall.n2.x){
				g2.fillRect(MARGIN + wall.n1.x * (TILESIZE+WALLSIZE), this.getHeight() - (MARGIN + wall.n1.y * (TILESIZE+WALLSIZE)), TILESIZE, WALLSIZE);
			}
			else
			{
				g2.fillRect(MARGIN + wall.n2.x * (TILESIZE+WALLSIZE), this.getHeight() - (MARGIN + wall.n2.y * (TILESIZE+WALLSIZE)), TILESIZE, WALLSIZE);
			}
			
		}
	}
	
	public static Color getColor(int i){
		Color res;
		switch(i){
		case 1 : res = Color.green; break;
		case 2 : res = Color.cyan; break;
		case 3 : res = Color.pink; break;
		default : res = Color.black;
		}
		
		return res;
	}

}
